package pacBiopackage;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

import tools.kmer.KmerSet_binary;
import tools.kmer.kmerFunctions;

public class PacBioSeedFinding 
{
	private PacBioReadContainer read = null;
	private KmerSet_binary ks=null;
	
	public PacBioSeedFinding(PacBioReadContainer read,KmerSet_binary ks) 
	{
		this.read = read;
		this.ks=ks;
		
	}
	
	private int checkKmerDistribution(String kmer)
	{
		if(kmer.equals(""))
		{
			return 0;
		}
		String kmerToUse = kmerFunctions.kmerToUse(kmer);
		BitSet bS = ks.getUtils().stringToBitSet(kmerToUse);
		//BitSet bS = ks.getUtils().stringToBitSet(kmerFunctions.kmerToUse(kmer.toString()));
		if(ks.exists(kmerToUse))
		{
			return(ks.getNumber(bS));
		}
		else
		{
			return 0;
		}
	}
	
	public boolean findSeed()
	{
		List<Integer> notCorrected = new ArrayList<Integer>();
		for(int i=0; i+ks.size()+1<= read.getOrginalSequence().length();++i)
		{
			//build kmer
			String kmer = read.getOrginalSequence().substring(i,i+ks.size()+1);
			if(checkKmerDistribution(kmer.toString()) !=0)
			{
				//yes we found one seed -> build new not corrected index
				
				for(int j=0; j <i;++j)
				{
					notCorrected.add(j);
				}
				for(int j=i+ks.size()+1; j <read.getOrginalSequence().length();++j)
				{
					notCorrected.add(j);
				}
				break;
			}
		}
		if(notCorrected.size()==0)
		{
			return false;
		}
		else
		{
			read.setNotCorrectedPositions(notCorrected);
			return true;
		}
	}
}
